<?php
class User_model extends BZ_Model
{
    protected $table = 'user';
    
    public function findByIds($data)
    {
        if ( isset($data['uid']) ) {
            if ( is_array($data['uid']) ) {
                $this->db->where_in('uid', $data['uid']);
            } else {
                $this->db->where('uid', $data['uid']);
            }
        }
        if ( isset($data['user_name']) ) {
            $this->db->where_in('user_name', $data['user_name']);
        }
        if ( isset($data['user_type']) ) {
            $this->db->where('user_type & ' . $data['user_type'] . '=', $data['user_type']);
        }
    
        return $this->db->get($this->table);
    }
    
    public function login($postData)
    {
        $username = trim(addslashes($postData['username']));
        $this->db->where("(`user_name`='{$username}' OR `mobile_phone`='{$username}')");
        $this->db->where('pw', md5($postData['password']));
        //$this->db->where('user_type & '.UTID_PROVIDER.'!=', UTID_PROVIDER);//非供应商帐号登录
        return $this->db->get($this->table);
    }

    /**
     * 快速登录验证
     * cyl
     */
    public function quick_login($data = [])
    {
        if (!isset($data['mobile_phone'])) {
            echo json_encode(array(
                'status' => false,
                'msg'    => '手机号码格式有误'
            ));exit;
        }
        if (!isset($data['verify'])) {
            echo json_encode(array(
                'status' => false,
                'msg'    => '请输入动态密码'
            ));exit;
        }
        $res = $this->db->select(array('flag', 'uid', 'user_name', 'alias_name', 'parent_id', 'extra', 'user_type', 'personal_photo'))
            ->where(array('mobile_phone' => $data['mobile_phone']))
            ->get($this->table)
            ->row(0);
        return $res;
    }

    public function get_data($w=array(), $f='*')
    {
        $this->db->select($f);
        $this->check_where($w);
        return $this->db->get($this->table);
    }

    private function check_where($w=array())
    {
        if (!empty($w['user'])) {
            $this->db->where("(`user_name`='{$w['user']}' OR `mobile_phone`='{$w['user']}')");
        }
        if (!empty($w['password'])) {
            $this->db->where('pw', $w['password']);
        }
        if (!empty($w['client_time'])) {
            $this->db->where('client_time', $w['client_time']);
        }
    }

    public  function visitCount($uid)
    {
        $this->db->set('login_count', 'login_count+1', false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 验证用户名
     * @param $userName
     * @return mixed
     */
    public function validateName($userName)
    {
        $this->db->where('user_name', $userName);
        return $this->db->get($this->table);
    }

    /**
     * 验证手机号码
     * @param $mobilePhone
     * @return mixed
     */
    public function validateMobilePhone($mobilePhone)
    {
        $this->db->where('mobile_phone', $mobilePhone);
        return $this->db->get($this->table);
    }

    /**
     * 注册时保存数据
     * @param array $postData
     * @param int $parent_id
     * @return mixed
     */
    public function insertUser($postData=array(), $parent_id=UTID_BEIZHU)
    {
        $data = array(
            'mobile_phone'   => '',
            'pw'             => '',
            'user_type'      => UTID_CUSTOMER,
            'parent_id'      => $parent_id,
            'owner_id'       => $parent_id,
            'sms'            => 0,
            'login_count'    => 1
        );
        if (!empty($postData['mobile_phone'])) {
            $data['mobile_phone'] = $postData['mobile_phone'];
        }
        $data['user_name'] = !empty($postData['user_name']) ? trim($postData['user_name']) : $data['mobile_phone'];
        $data['alias_name'] = !empty($postData['alias_name']) ? trim($postData['alias_name']) : $data['mobile_phone'];
        if (!empty($postData['password'])) {
            $data['pw'] = md5($postData['password']);
        }
        if (!empty($postData['user_type'])) {
            $data['user_type'] = $postData['user_type'];
        }
        if (!empty($postData['owner_id'])) {
            $data['owner_id'] = $postData['owner_id'];
        }
        if (!empty($postData['personal_photo'])) {
            $data['personal_photo'] = $postData['personal_photo'];
        }
        if (isset($postData['extra'])) {
            $data['extra'] = $postData['extra'];
        }
        if (isset($postData['sms'])) {
            $data['sms'] = $postData['sms'];
        }
        if (isset($postData['key'])) {
            $data['key'] = $postData['key'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function modifyPassword($postData=array())
    {
        $data = array(
            'pw' => md5($postData['password']),
        );
        $this->db->where('user_name', $postData['username']);
        return $this->db->update($this->table, $data);
    }

    /**
     * 验证用户名称
     * @param $user_name
     * @return mixed
     */
    public function validateExistUser($user_name)
    {
        $this->db->where('user_name', $user_name);
        $this->db->where('user_type &'.UTID_SELLER.'=', UTID_SELLER);
        return $this->db->count_all_results($this->table);
    }
}